package com.imooc.postprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.*;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Constructor;

@SuppressWarnings("serial")
@Configuration
public class AllBeanPostProcessor implements
        BeanPostProcessor,
        InstantiationAwareBeanPostProcessor,
        SmartInstantiationAwareBeanPostProcessor,
        MergedBeanDefinitionPostProcessor,
        DestructionAwareBeanPostProcessor,
        BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: DestructionAwareBeanPostProcessor.postProcessBeforeDestruction");
    }

    @Override
    public boolean requiresDestruction(Object bean) {
        System.out.println(bean.getClass().getSimpleName() + "调用了: DestructionAwareBeanPostProcessor.requiresDestruction");
        return false;
    }

    @Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        System.out.println(beanName + "调用了: MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition");

    }

    @Override
    public void resetBeanDefinition(String beanName) {
        System.out.println(beanName + "调用了: MergedBeanDefinitionPostProcessor.resetBeanDefinition");

    }

    @Override
    public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: SmartInstantiationAwareBeanPostProcessor.predictBeanType");

        return null;
    }

    @Override
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: SmartInstantiationAwareBeanPostProcessor.determineCandidateConstructors");
        return null;
    }

    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: SmartInstantiationAwareBeanPostProcessor.getEarlyBeanReference");
        return bean;
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: InstantiationAwareBeanPostProcessor.postProcessProperties");
        return null;
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: BeanPostProcessor.postProcessBeforeInitialization");
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + "调用了: BeanPostProcessor.postProcessAfterInitialization");
        return null;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("调用了: BeanFactoryPostProcessor.postProcessBeanFactory");

    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("调用了: BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry");

    }


}
